package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;
    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan byId = getById(id);
        BeanUtils.copyProperties(byId,nursingPlanVo);
        //根据护理计划id查询中间表数据集合，然后进行数据封装
        List<NursingProjectPlan> list = iNursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, byId.getId()));
        //类型转换
        List<NursingProjectPlanVo> list1 = list.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(nursingProjectPlan.getProjectId()+"");
            nursingProjectPlanVo.setId(nursingProjectPlan.getId());
            nursingProjectPlanVo.setPlanId(nursingProjectPlan.getPlanId());
            nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle()+"");
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(list1);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        //int row = nursingPlanMapper.insertNursingPlan(nursingPlan);
        boolean save = this.save(nursingPlan);
        //nursingProjectPlanMapper.baschInster(Long.valueOf(nursingPlan.getId()),nursingPlanDto.getProjectPlans());
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(projectPlans != null){
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId((Long.valueOf(nursingPlan.getId())));
            }
        }
        iNursingProjectPlanService.saveBatch(projectPlans);
        return save?1:0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(projectPlans !=null){
            //护理项目不为空，则现删除当前项目

                iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,nursingPlanDto.getId()));
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(nursingPlanDto.getId());
                boolean save = iNursingProjectPlanService.save(projectPlan);
            }
        }
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        boolean b = this.updateById(nursingPlan);
        return b?1:0;

    }



    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Long id) {
        //要想删除当前护理计划中的护理项目，启用状态下不能删除
        NursingPlan byId = this.getById(id);
        if (byId.getStatus() != 1) {
            //根据planid查询出当前该id下的护理项目集合
            List<NursingProjectPlan> list = iNursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
            if (list != null) {
                iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
            }

            boolean b = this.removeById(id);
            return b ? 1 : 0;
        }else {
            return 0;
        }
    }
}
